

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>cup.cache &mdash; Cup 3.2.19 documentation</title>
  

  
  
  
  

  

  
  
    

  

  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" /> 

  
  <script src="../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../index.html" class="icon icon-home"> Cup
          

          
          </a>

          
            
            
              <div class="version">
                3.2.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../cup.html">cup package</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">Cup</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../index.html">Module code</a> &raquo;</li>
        
      <li>cup.cache</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for cup.cache</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python</span>
<span class="c1"># -*- coding: utf-8 -*</span>
<span class="c1"># Copyright: [CUP] - See LICENSE for details.</span>
<span class="c1"># Authors: Guannan Ma (@mythmgn),</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">:description:</span>
<span class="sd">    decorators related module</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">import</span> <span class="nn">collections</span>
<span class="kn">import</span> <span class="nn">contextlib</span>

<span class="kn">import</span> <span class="nn">cup</span>
<span class="kn">from</span> <span class="nn">cup.util</span> <span class="k">import</span> <span class="n">thread</span>


<div class="viewcode-block" id="KvCache"><a class="viewcode-back" href="../../cup.html#cup.cache.KvCache">[docs]</a><span class="k">class</span> <span class="nc">KvCache</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Key-Value Cache object.</span>

<span class="sd">    You can use function set/get to access KeyValue Cache.</span>

<span class="sd">    When a k-v is hit by function **get**,</span>
<span class="sd">    the expire_sec will be expanded to 2 * (expire_sec)</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">_STAT</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">namedtuple</span><span class="p">(</span>
        <span class="s1">&#39;kvcache_stat&#39;</span><span class="p">,</span> <span class="s1">&#39;key_num expired_num&#39;</span>
    <span class="p">)</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">maxsize</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :param maxsize:</span>
<span class="sd">            not used yet, for future use</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_maxsize</span> <span class="o">=</span> <span class="n">maxsize</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_kv_data</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_lock</span> <span class="o">=</span> <span class="n">thread</span><span class="o">.</span><span class="n">RWLock</span><span class="p">()</span>

    <span class="nd">@contextlib</span><span class="o">.</span><span class="n">contextmanager</span>
    <span class="k">def</span> <span class="nf">_lock_release</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">b_rw_lock</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">b_rw_lock</span> <span class="ow">is</span> <span class="kc">True</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_lock</span><span class="o">.</span><span class="n">acquire_writelock</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_lock</span><span class="o">.</span><span class="n">acquire_readlock</span><span class="p">()</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">yield</span>
        <span class="c1"># pylint: disable=W0703</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
            <span class="n">cup</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s1">&#39;something happend in cache:</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">error</span><span class="p">)</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">b_rw_lock</span> <span class="ow">is</span> <span class="kc">True</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_lock</span><span class="o">.</span><span class="n">release_writelock</span><span class="p">()</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_lock</span><span class="o">.</span><span class="n">release_readlock</span><span class="p">()</span>

<div class="viewcode-block" id="KvCache.set"><a class="viewcode-back" href="../../cup.html#cup.cache.KvCache.set">[docs]</a>    <span class="k">def</span> <span class="nf">set</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">kvdict</span><span class="p">,</span> <span class="n">expire_sec</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        set cache with kvdict</span>
<span class="sd">        ::</span>

<span class="sd">            {</span>
<span class="sd">                &#39;key1&#39;: &#39;value1&#39;,</span>
<span class="sd">                &#39;key2&#39;: &#39;value2&#39;,</span>
<span class="sd">                ....</span>
<span class="sd">            }</span>

<span class="sd">        :param kvdict:</span>
<span class="sd">            kvdict is a dict that contains your cache.</span>
<span class="sd">        :param expire_sec:</span>
<span class="sd">            if expire_sec is None, the cache will never expire.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">expire_value</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="n">expire_sec</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">expire_value</span> <span class="o">=</span> <span class="n">expire_sec</span> <span class="o">+</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
        <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">_lock_release</span><span class="p">(</span><span class="n">b_rw_lock</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">kvdict</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">key</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_kv_data</span><span class="p">:</span>
                    <span class="n">cup</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span>
                        <span class="s1">&#39;KvCache: Key:</span><span class="si">{0}</span><span class="s1"> updated.&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
                    <span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_kv_data</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">kvdict</span><span class="p">[</span><span class="n">key</span><span class="p">],</span> <span class="n">expire_value</span><span class="p">)</span></div>

<div class="viewcode-block" id="KvCache.get"><a class="viewcode-back" href="../../cup.html#cup.cache.KvCache.get">[docs]</a>    <span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get your cache with key.</span>
<span class="sd">        If the cache is expired, it will return None.</span>
<span class="sd">        If the key does not exist, it will return None.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">_lock_release</span><span class="p">(</span><span class="n">b_rw_lock</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">key</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_kv_data</span><span class="p">:</span>
                <span class="k">return</span> <span class="kc">None</span>
            <span class="n">value</span><span class="p">,</span> <span class="n">expire_sec</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_kv_data</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">expire_sec</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">&gt;</span> <span class="n">expire_sec</span><span class="p">:</span>
                <span class="k">return</span> <span class="kc">None</span>
            <span class="n">cup</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;key:</span><span class="si">%s</span><span class="s1"> of kvCache fetched.&#39;</span> <span class="o">%</span> <span class="n">key</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_kv_data</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">expire_sec</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">value</span>
        <span class="k">return</span> <span class="kc">None</span></div>

<div class="viewcode-block" id="KvCache.get_expired_keys"><a class="viewcode-back" href="../../cup.html#cup.cache.KvCache.get_expired_keys">[docs]</a>    <span class="k">def</span> <span class="nf">get_expired_keys</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;get expired key of keys&quot;&quot;&quot;</span>
        <span class="n">expired_keys</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">keys</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_kv_data</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">keys</span><span class="p">:</span>
            <span class="n">expire_sec</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_kv_data</span><span class="p">[</span><span class="n">key</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">expire_sec</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">&gt;</span> <span class="n">expire_sec</span><span class="p">:</span>
                <span class="n">expired_keys</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">expired_keys</span></div>

<div class="viewcode-block" id="KvCache.cleanup_expired"><a class="viewcode-back" href="../../cup.html#cup.cache.KvCache.cleanup_expired">[docs]</a>    <span class="k">def</span> <span class="nf">cleanup_expired</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Delete all expired items</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">expired_keys</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">_lock_release</span><span class="p">(</span><span class="n">b_rw_lock</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
            <span class="n">expired_keys</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_expired_keys</span><span class="p">()</span>
            <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">expired_keys</span><span class="p">:</span>
                <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_kv_data</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
                <span class="n">cup</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;key:</span><span class="si">%s</span><span class="s1"> cleaned up&#39;</span> <span class="o">%</span> <span class="n">key</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">expired_keys</span></div>

<div class="viewcode-block" id="KvCache.get_expired"><a class="viewcode-back" href="../../cup.html#cup.cache.KvCache.get_expired">[docs]</a>    <span class="k">def</span> <span class="nf">get_expired</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :return:</span>
<span class="sd">            A dict.</span>
<span class="sd">            Return expired items. Return type is a dict</span>
<span class="sd">            ::</span>

<span class="sd">                {</span>
<span class="sd">                    &#39;key&#39; : (value, expire_time)</span>
<span class="sd">                }</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">kvlist</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">_lock_release</span><span class="p">(</span><span class="n">b_rw_lock</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
            <span class="n">keys</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_expired_keys</span><span class="p">()</span>
            <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">keys</span><span class="p">:</span>
                <span class="n">value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_kv_data</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
                <span class="n">kvlist</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">value</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">kvlist</span></div>

<div class="viewcode-block" id="KvCache.stat"><a class="viewcode-back" href="../../cup.html#cup.cache.KvCache.stat">[docs]</a>    <span class="k">def</span> <span class="nf">stat</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        :return:</span>
<span class="sd">            a tuple with (item_num, expired_num)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">_lock_release</span><span class="p">(</span><span class="n">b_rw_lock</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
            <span class="n">key_num</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_kv_data</span><span class="p">)</span>
            <span class="n">expired_num</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_expired_keys</span><span class="p">())</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">key_num</span><span class="p">,</span> <span class="n">expired_num</span><span class="p">)</span></div>

<div class="viewcode-block" id="KvCache.clear"><a class="viewcode-back" href="../../cup.html#cup.cache.KvCache.clear">[docs]</a>    <span class="k">def</span> <span class="nf">clear</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        remove all kv cache inside.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">_lock_release</span><span class="p">(</span><span class="n">b_rw_lock</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
            <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_kv_data</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_kv_data</span> <span class="o">=</span> <span class="p">{}</span></div></div>

<span class="c1"># vi:set tw=0 ts=4 sw=4 nowrap fdm=indent</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2020, CUP-DEV.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../',
            VERSION:'3.2.19',
            LANGUAGE:'en',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../_static/doctools.js"></script>
      <script type="text/javascript" src="../../_static/language_data.js"></script>

  

  <script type="text/javascript" src="../../_static/js/theme.js"></script>

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>